package com.monitor.display_platform.core.common;


import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化json的时候,如果是null的对象,key也会消失
public class ServerResponse<T> implements Serializable {

    private boolean ret;
    private String msg;
    private T data;

    private ServerResponse(boolean ret){
        this.ret = ret;
    }

    private ServerResponse(boolean ret, T data){
        this.ret = ret;
        this.data = data;
    }

    private ServerResponse(boolean ret, String msg, T data){
        this.ret = ret;
        this.msg = msg;
        this.data = data;
    }

    private ServerResponse(boolean ret, String msg){
        this.ret = ret;
        this.msg = msg;
    }

    public boolean getRet(){
        return ret;
    }
    public T getData(){
        return data;
    }
    public String getMsg(){
        return msg;
    }

    public Map<String , Object> toMap(){
        Map<String,Object> res = new HashMap<>();
        res.put("ret",ret);
        res.put("msg",msg);
        res.put("data",data);
        return res;
    }

    public static <T> ServerResponse<T> success(){
        return new ServerResponse<T>(true);
    }

    public static <T> ServerResponse<T> successMsg(String msg){
        return new ServerResponse<T>(true,msg);
    }

    public static <T> ServerResponse<T> success(T data){
        return new ServerResponse<T>(true,data);
    }

    public static <T> ServerResponse<T> success(String msg,T data){
        return new ServerResponse<T>(true,msg,data);
    }

    /**指定异常*/
    public static <T> ServerResponse<T> fail(String msg){
        return new ServerResponse<T>(false,msg);
    }
}

